package com.htu.courseService.courseManage.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.htu.courseService.common.vo.R;
import com.htu.courseService.courseManage.service.CoursesService;
import com.htu.courseService.courseManage.vo.CourseQuery;
import com.htu.courseService.courseManage.vo.CoursesDetail;
import com.htu.courseService.courseManage.vo.CoursesDto;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/courseManage")
@RequiredArgsConstructor
@Slf4j
public class CourseManageController {

    private final CoursesService coursesService;

    /**
     * 新增一个课程
     * @param coursesDto
     * @return
     */
    @PostMapping("/create")
    public R<?> create(@RequestBody CoursesDto coursesDto){
        coursesService.saveCourses(coursesDto);
        return R.success("新建课程成功");
    }

    /**
     * 删除一个课程
     * @param courseId
     * @return
     */
    @DeleteMapping("/delete")
    public R<?> delete(Integer courseId){
        coursesService.deleteCourses(courseId);
        return R.success("删除课程成功");
    }

    /**
     * 获取分页的课程列表
     * @param courseQuery
     * @return
     */
    @PostMapping("/getCoursePage")
    public R<IPage<CoursesDto>> getCoursePage(@RequestBody  CourseQuery courseQuery){
        IPage<CoursesDto> coursesDtoIPage = coursesService.getCoursePage(courseQuery);
        return R.success(coursesDtoIPage);
    }

    /**
     * 更新课程信息
     * @param coursesDto
     * @return
     */
    @PutMapping("/update")
    public R<?> update(@RequestBody CoursesDto coursesDto){
        coursesService.updateCourseInfo(coursesDto);
        return R.success("修改成功");
    }

    /**
     * 通过主键id获取课程详细信息
     * @param courseId
     * @return
     */
    @GetMapping("/getCourseDetail")
    public R<CoursesDetail> getCourseDetail(Integer courseId){
        CoursesDetail courseDetail = coursesService.getCourseDetail(courseId);
        return R.success(courseDetail);
    }
}
